package com.xbao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xbao.core.component.page.Pagination;
import com.xbao.core.util.MyBeanUtil;
import com.xbao.dao.AskAbsentExtraMapper;
import com.xbao.dao.AskAbsentMapper;
import com.xbao.model.AskAbsent;
import com.xbao.model.AskAbsentExample;
import com.xbao.model.req.AskAbsentReq;
import com.xbao.model.req.CheckPeriodReq;
import com.xbao.model.req.ResultAddition;
import com.xbao.model.vo.AskAbsentVo;
import com.xbao.model.vo.ClsVo;
import com.xbao.model.vo.EmpVo;
import com.xbao.model.vo.StuVo;
import com.xbao.service.AskAbsentService;
import com.xbao.service.EmpService;
import com.xbao.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qingshengzheng on 18/10/15.
 */
@Service
public class AskAbsentServiceImpl implements AskAbsentService {
    @Autowired
    private AskAbsentMapper absentMapper;
    @Autowired
    private AskAbsentExtraMapper extraMapper;


    @Resource
    private StuService stuService;

    @Override
    public void addAskAbsent(AskAbsentReq req) {
        req.setCreatetime(System.currentTimeMillis());
        absentMapper.insert(req);
    }

    @Override
    public void deleteAskAbsent(AskAbsentReq req) {
        absentMapper.deleteByPrimaryKey(req.getId());
    }

    @Override
    public List<AskAbsentVo> queryAskAbsentList(AskAbsentReq askAbsentReq) {
        AskAbsentExample askAbsentExample =new AskAbsentExample();
        AskAbsentExample.Criteria criteria=askAbsentExample.createCriteria();
        if(askAbsentReq.getPager()!=null)
            PageHelper.startPage(askAbsentReq.getPager().getPageNo(), askAbsentReq.getPager().getPageSize());
        if  (askAbsentReq.getSid()!=null)
            criteria.andSidEqualTo(askAbsentReq.getSid());

        List<AskAbsent> askAbsents = absentMapper.selectByExample(askAbsentExample);
        List<AskAbsentVo> askAbsentVos= MyBeanUtil.copyList(askAbsents,AskAbsentVo.class);
        resultAdd(askAbsentVos,askAbsentReq.getRadd());
        return askAbsentVos;
    }

    @Override
    public List<AskAbsentVo> queryAskAbsentMainPage(AskAbsentReq askAbsentReq) {

        if(askAbsentReq.getPager()!=null)
            PageHelper.startPage(askAbsentReq.getPager().getPageNo(), askAbsentReq.getPager().getPageSize());
        List<AskAbsentVo> askAbsents = extraMapper.queryList(askAbsentReq);
        return askAbsents;
    }

    public void resultAdd(List<AskAbsentVo> vos, ResultAddition resultAddition) {
        if(resultAddition==null) return;
        for (AskAbsentVo vo : vos) {
            if (resultAddition.getIename()) {
                StuVo ev =stuService.detail(vo.getSid());
                if (ev != null){
                    vo.setStuVo(ev);
                }
            }

        }


    }




    public CheckPeriodReq combCheck(AskAbsent askAbsent) {
        CheckPeriodReq period = new CheckPeriodReq();
        period.setChecktime(System.currentTimeMillis());
        period.setCid(askAbsent.getCid());
        period.setOrgid(askAbsent.getOrgid());
        period.setStatus(2);
        return period;
    }

    @Override
    public void confirmAskAbsent(AskAbsentReq req) {
        CheckPeriodReq checkPeriodReq = combCheck(req);
        //删除
        absentMapper.deleteByPrimaryKey(req.getId());
    }


}
